#!/bin/bash

# source /etc/profile
appName=CavaSvrProxy
target=cava3svrproxy

function del()
{
    #rm -rf main 
    rm -rf ${target}
    rm -rf ${target}.tar.gz
}

GIT_BRANCH=`git rev-parse --abbrev-ref HEAD || echo`
GIT_SHA=`git rev-parse --short HEAD || echo`
BUILD_AT=`date "+%Y-%m-%dT%H:%M:%S"`

arch_type=$1
if [ "$arch_type" == "" ];then
    arch_type=amd64
    is_amd64=`cat /proc/version | grep " x86_64\|amd64"`
    if [ "$is_amd64" == "" ];then
         arch_type=arm64
    fi
fi

mgr_scripts=*.sh

os_type=$2
if [ "$os_type" == "" ];then
    os_type=linux
    is_linux=`uname -a | grep -i linux`
    if [ "$is_linux" == "" ];then
        os_type=darwin
    fi
elif [ "$os_type" == "windows" ];then
    appName=CavaSvrProxy.exe
    mgr_scripts=start.bat
fi

echo "arch type: $arch_type, os type: $os_type"

del
cmd=`printf "CGO_ENABLED=0 GOOS=$os_type GOARCH=$arch_type go build -ldflags \"-X main.GitBranch=${GIT_BRANCH} -X main.GitSHA=${GIT_SHA} -X main.BuildAt=${BUILD_AT}\" -o ${appName} -mod=vendor"`
echo "cmd: $cmd"
echo $cmd | bash
del

mkdir ${target}
cp -rf ${appName} ${target}
cp -rf conf ${target}
cp -rf ${mgr_scripts} ${target}
cp -rf pem ${target}
cp -rf *.tmpl ${target}
git log -1 .. > ${target}/version.info

tar -czvf ${target}.tar.gz ${target}
rm -rf ${target}
rm -rf ${appName}

echo "pack ${appName} to ${target}.tar.gz succeed."
